import {createApp} from 'vue'
import App from './App.vue'
import router from "@/router";
import 'element-plus/dist/index.css'
import ElementPlus from "element-plus";
import locale from 'element-plus/es/locale/lang/zh-cn'
import request from './utils/http/Request';
import * as ElIconModules from '@element-plus/icons-vue';


const app = createApp(App);
// 全局注册 request 模块
app.config.globalProperties.$request = request;
// 全局注册icon
for (const [key, component] of Object.entries(ElIconModules)) {
    app.component(key, component)
}
// 定义长按指令
const longpress = {
    mounted(el, binding) {
        let pressTimer = null;

        const start = (e) => {
            if (e.type === 'click' && e.button !== 0) {
                return;
            }
            if (pressTimer === null) {
                pressTimer = setTimeout(() => {
                    handler(e);
                }, 500); // 1秒钟长按时间
            }
        };

        const cancel = () => {
            if (pressTimer !== null) {
                clearTimeout(pressTimer);
                pressTimer = null;
            }
        };

        const handler = (e) => {
            binding.value(e);
        };

        el.addEventListener('mousedown', start);
        el.addEventListener('touchstart', start);
        el.addEventListener('click', cancel);
        el.addEventListener('mouseout', cancel);
        el.addEventListener('touchend', cancel);
        el.addEventListener('touchcancel', cancel);
        el._start = start;
        el._cancel = cancel;
    },
    unmounted(el) {
        el.removeEventListener('mousedown', el._start);
        el.removeEventListener('touchstart', el._start);
        el.removeEventListener('click', el._cancel);
        el.removeEventListener('mouseout', el._cancel);
        el.removeEventListener('touchend', el._cancel);
        el.removeEventListener('touchcancel', el._cancel);
    }
};
app.directive('longpress', longpress);
app.use(ElementPlus,{ locale });
app.use(router);
app.mount('#app');

